package com.service;

import com.bean.*;
import com.dao.*;

import java.util.List;
import java.util.regex.Pattern;

/**
 * TODO提供管理员的业务操作
 *
 */
public class AdminService {
    /**
     * 该方法主要使用正则表达式来判断字符串是否是数字
     *
     */
    public boolean judgeContainsStr(String cardNum) {
        boolean isNum = cardNum.matches("[0-9]+");
        return isNum;
    }
    //判断浮点数（double和float）
    public boolean isDouble(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(str).matches();
    }
    public boolean addEmployee(Employee employee){
        String id=employee.getId();
        EmployeeDao employeeDao=new EmployeeDao();
        if(employeeDao.getEmployee(id)!=null)
            return false;
        employeeDao.addEmployee(employee);
        return true;
    }
    public void deleteEmployee(String id){
        EmployeeDao employeeDao=new EmployeeDao();
        employeeDao.deleteEmployee(id);
    }
    public boolean updateEmployee(Employee employee){
        if (!getDept(employee.getDeptid()))
            return false;
        EmployeeDao employeeDao=new EmployeeDao();
        employeeDao.updateEmployee(employee);
        return true;
    }
    public List<Employee> getEmployeeListByDept(String deptid){
        EmployeeDao employeeDao=new EmployeeDao();
        return employeeDao.getEmployeeListByDept(deptid);
    }
    public List<Employee> getEmployeeList(){
        EmployeeDao employeeDao=new EmployeeDao();
        return employeeDao.getEmployeeList();
    }
    public Employee getEmployee(String id){
        EmployeeDao employeeDao=new EmployeeDao();
        return employeeDao.getEmployee(id);
    }
    public Admin getAdmin(String id){
        AdminDao adminDao=new AdminDao();
        return adminDao.getAdmin(id);
    }
    public boolean checkLogin(String id,String password){
        if (getAdmin(id)==null)
            return false;
        if (password.equals(getAdmin(id).getPassword()))
            return true;
        return false;
    }
    public void addDept(Department department){
        DepartmentDao departmentDao=new DepartmentDao();
        departmentDao.addDepartment(department);
    }
    public boolean getDept(String deptid){
        DepartmentDao departmentDao=new DepartmentDao();
        return departmentDao.getDepartment(deptid);
    }
    public List<Department> getDept(){
        DepartmentDao departmentDao=new DepartmentDao();
        return departmentDao.getDepartmentList();
    }
    public void updateDept(Department department){
        DepartmentDao departmentDao=new DepartmentDao();
        departmentDao.updateDepartment(department);
    }
    public void deleteDept(String dnumber){
        DepartmentDao departmentDao=new DepartmentDao();
        departmentDao.deleteDepartment(dnumber);
    }

    public void addSalary(Salary salary){
        SalaryDao salaryDao=new SalaryDao();
        salaryDao.addSalary(salary);
    }
    public List<Salary> getSalary(String year,String month){
        SalaryDao salaryDao=new SalaryDao();
        return salaryDao.getSalary(year,month);
    }
    public List<Salary> getSalary(String year){
        SalaryDao salaryDao=new SalaryDao();
        return salaryDao.getSalary(year);
    }
    public List<Salary> getSalaryByUser(String id,String year,String month){
        SalaryDao salaryDao=new SalaryDao();
        System.out.print(111111);
        return salaryDao.getSalaryListByUser(id,year,month);
    }
    public List<Salary> getSalaryByUserYear(String id,String year){
        SalaryDao salaryDao=new SalaryDao();
        return salaryDao.getSalaryListByUserYear(id,year);
    }
    public List<Salary> getSalaryByDept(String deptid,String year,String month){
        SalaryDao salaryDao=new SalaryDao();
        return salaryDao.getSalaryListByDept(deptid,year,month);
    }

    public List<Salary> getSalaryByDeptYear(String deptid,String year){
        SalaryDao salaryDao=new SalaryDao();
        return salaryDao.getSalaryListByDeptYear(deptid,year);
    }
    public double deptmonthsales(String deptid,String year,String month){
        SalaryDao salaryDao=new SalaryDao();
        double sales = 0;
        List<Salary> salarys = salaryDao.getSalaryListByDept(deptid,year,month);
        for (Salary salary:salarys
             ) {
            sales+=salary.getSalesVolumn();
        }
        return sales;
    }
    public double deptannualsales(String deptid,String year){
        SalaryDao salaryDao=new SalaryDao();
        double sales = 0;
        List<Salary> salarys = salaryDao.getSalaryListByDeptYear(deptid,year);
        for (Salary salary:salarys
        ) {
            sales+=salary.getSalesVolumn();
        }
        return sales;
    }
    public double annualsales(String id,String year){
        SalaryDao salaryDao=new SalaryDao();
        double sales = 0;
        List<Salary> salarys = salaryDao.getSalaryListByUserYear(id,year);
        for (Salary salary:salarys
        ) {
            sales+=salary.getSalesVolumn();
        }
        return sales;
    }

}
